package cn.zwxky.android.staff.Jurisdiction.Marketplace;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

import cn.zwxky.android.staff.Approve.TaskDetailsChatActivity;
import cn.zwxky.android.staff.Jurisdiction.Furlough.LeaveRejectActivity;
import cn.zwxky.android.staff.Jurisdiction.Furlough.ManageLeaveClickEvent2;
import cn.zwxky.android.staff.Login.ForgetBean;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.NoDoubleClickUtils;
import cn.zwxky.android.staff.Utils.PictureActivity;
import cn.zwxky.android.staff.Utils.URLString;

/**
 * 项目审批详情
 * Created by Administrator on 2017/3/31 0031.
 */
@ContentView(R.layout.aty_task_approve_details)
public class TaskApproveDetailsActivity extends LsfbActivity {

    @ViewInject(R.id.aty_manage_leave_bottom)
    private LinearLayout leave_bottom;

    /**
     * 头像
     */
    @ViewInject(R.id.aty_task_details_img_head)
    private SimpleDraweeView img_head;

    /**
     * 名字
     */
    @ViewInject(R.id.aty_text_name)
    private TextView text_name;

    /**
     * 电话
     */
    @ViewInject(R.id.aty_text_phone)
    private TextView text_phone;

    /**
     * 公司
     */
    @ViewInject(R.id.aty_text_gongsi)
    private TextView text_gongsi;

    /**
     * 项目名称
     */
    @ViewInject(R.id.aty_text_title)
    private TextView text_title;

    /**
     * 备注
     */
    @ViewInject(R.id.aty_text_beizhu)
    private TextView text_beizhu;

    /**
     * 项目类型
     */
    @ViewInject(R.id.aty_text_type)
    private TextView text_type;

    /**
     * 市场部状态
     */
    @ViewInject(R.id.aty_task_details_state1)
    private ImageView state1;

    /**
     * 总经理状态
     */
    @ViewInject(R.id.aty_task_details_state2)
    private ImageView state2;

    /**
     * 经营部状态
     */
    @ViewInject(R.id.aty_task_details_state3)
    private ImageView state3;

    /**
     * 市场部头像
     */
    @ViewInject(R.id.item_leave_img_head1)
    private SimpleDraweeView img_head1;

    /**
     * 市场部名字
     */
    @ViewInject(R.id.aty_task_details_text_name1)
    private TextView text_name1;

    /**
     * 市场部状态
     */
    @ViewInject(R.id.aty_task_details_text_state1)
    private TextView text_state1;

    /**
     * 市场部讨论
     */
    @ViewInject(R.id.aty_task_details_text_chat)
    private TextView text_chat;

    /**
     * 市场部时间
     */
    @ViewInject(R.id.aty_task_details_text_time1)
    private TextView text_time1;

    /**
     * 总经理头像
     */
    @ViewInject(R.id.item_leave_img_head2)
    private SimpleDraweeView img_head2;

    /**
     * 总经理名字
     */
    @ViewInject(R.id.aty_task_details_text_name2)
    private TextView text_name2;

    /**
     * 总经理状态
     */
    @ViewInject(R.id.aty_task_details_text_state2)
    private TextView text_state2;

    /**
     * 总经理时间
     */
    @ViewInject(R.id.aty_task_details_text_time2)
    private TextView text_time2;

    /**
     * 经营部头像
     */
    @ViewInject(R.id.item_leave_img_head3)
    private SimpleDraweeView img_head3;

    /**
     * 经营部名字
     */
    @ViewInject(R.id.aty_task_details_text_name3)
    private TextView text_name3;

    /**
     * 经营部状态
     */
    @ViewInject(R.id.aty_task_details_text_state3)
    private TextView text_state3;

    /**
     * 经营部时间
     */
    @ViewInject(R.id.aty_task_details_text_time3)
    private TextView text_time3;

    private String id;

    /**
     * 状态
     */
    private String status;

    /**
     * 三张图片
     */
    @ViewInject(R.id.aty_img_photo1)
    private SimpleDraweeView img_photo1;

    @ViewInject(R.id.aty_img_photo2)
    private SimpleDraweeView img_photo2;

    @ViewInject(R.id.aty_img_photo3)
    private SimpleDraweeView img_photo3;

    @ViewInject(R.id.aty_task_list)
    private LinearLayout atask_list;

    private String img1="";
    private String img2="";
    private String img3="";

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "项目审批");

        id = getIntent().getStringExtra("id");
    }

    @Override
    public void initEvent() {

    }

    @OnClick({R.id.item_leave1, R.id.aty_manage_leave_text_agree, R.id.aty_manage_leave_text_refuse,R.id.aty_img_photo1,R.id.aty_img_photo2,R.id.aty_img_photo3})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_leave1:
                if (LittleUtils.nodel.equals("1")|| LittleUtils.depart.equals("5")) {
                    //总经理
//                    startActivity(new Intent(this, MarketChatActivity.class).putExtra("id", id));
                    startActivity(new Intent(this, TaskDetailsChatActivity.class).putExtra("id", id));//也可以讨论了
                } else {
                    if (LittleUtils.depart.equals("1")) {
                        //市场部
                        startActivity(new Intent(this, TaskDetailsChatActivity.class).putExtra("id", id));
                    }
                }

                break;
            //同意
            case R.id.aty_manage_leave_text_agree:
                if (LittleUtils.nodel.equals("1")|| LittleUtils.depart.equals("5")) {
                    //总经理
                    if (!NoDoubleClickUtils.isDoubleClick()) {
                        if (!status.equals("2")) {
                            SN.SnackShow(text_state3, "您还不能这么做");
                            return;
                        }
                        podt2(1);
                    }
                } else {
                    if (LittleUtils.depart.equals("1")) {
                        //市场部
                        if (!NoDoubleClickUtils.isDoubleClick()) {
                            podt(1);
                        }
                    }
                }

                break;
            //拒绝
            case R.id.aty_manage_leave_text_refuse:
                if (LittleUtils.nodel.equals("1")|| LittleUtils.depart.equals("5")) {
                    //总经理
                    if (!NoDoubleClickUtils.isDoubleClick()) {
                        if (!status.equals("2")) {
                            SN.SnackShow(text_state3, "您还不能这么做");
                            return;
                        }
//                        podt2(2);
                        startActivity(new Intent(this, LeaveRejectActivity.class).putExtra("id", id).putExtra("type", "2"));
                    }
                } else {
                    if (LittleUtils.depart.equals("1")) {
                        //市场部
                        if (!NoDoubleClickUtils.isDoubleClick()) {
//                            podt(2);
                            startActivity(new Intent(this, LeaveRejectActivity.class).putExtra("id", id).putExtra("type", "3"));
                        }
                    }
                }
                break;
            case R.id.aty_img_photo1:
                startActivity(new Intent(this, PictureActivity.class).putExtra("url",img1));
                break;
            case R.id.aty_img_photo2:
                startActivity(new Intent(this, PictureActivity.class).putExtra("url",img2));
                break;
            case R.id.aty_img_photo3:
                startActivity(new Intent(this, PictureActivity.class).putExtra("url",img3));
                break;
        }
    }

    @Override
    public void initData() {
        getData();
    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);
        new BaseInternet().getData_Get(URLString.MANAGE_MYEAAINFO, hashMap, TaskApproveDetailsBean.class, new TaskApproveDetailsEvent<TaskApproveDetailsBean>(), true, true);
    }

    @EventAnnotation
    public void TaskApproveDetailsEvent(TaskApproveDetailsEvent<TaskApproveDetailsBean> event) {
        switch (event.getCode()) {
            case 200:
                img_head.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getImg()));
                text_name.setText(event.getData().getName());
                text_phone.setText(event.getData().getTel());
                text_gongsi.setText(event.getData().getCompany());
                text_title.setText(event.getData().getProname());
                if (event.getData().getContent().isEmpty()) {
                    text_beizhu.setVisibility(View.GONE);
                } else {
                    text_beizhu.setVisibility(View.VISIBLE);
                    text_beizhu.setText(event.getData().getContent());
                }

                text_type.setText(event.getData().getCla_name());
                status = event.getData().getStatus();

                //0未处理1同意2拒绝
                if (event.getData().getList().get(0).getStatus().equals("0")) {
                    state1.setImageResource(R.mipmap.img_zhong);
                    text_state1.setText("未处理");
                    if (LittleUtils.nodel.equals("1")|| LittleUtils.depart.equals("5")) {
                        leave_bottom.setVisibility(View.GONE);
                    }else {
                        leave_bottom.setVisibility(View.VISIBLE);
                    }
                    text_state1.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
                } else if (event.getData().getList().get(0).getStatus().equals("1")) {
                    state1.setImageResource(R.mipmap.img_yes);
                    text_state1.setText("同意");
                    leave_bottom.setVisibility(View.GONE);
                    text_state1.setTextColor(getResources().getColor(R.color.label_green));
                } else if (event.getData().getList().get(0).getStatus().equals("2")) {
                    state1.setImageResource(R.mipmap.img_no);
                    text_state1.setText("拒绝");
                    leave_bottom.setVisibility(View.GONE);
                    text_state1.setTextColor(getResources().getColor(R.color.label_red));
                }
                img_head1.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getList().get(0).getImg()));
                text_name1.setText(event.getData().getList().get(0).getName());
                text_chat.setText(event.getData().getList().get(0).getCount() + "条讨论");
                text_time1.setText(event.getData().getList().get(0).getTimes());

                //0未处理1同意2拒绝
                if (event.getData().getList().get(1).getStatus().equals("0")) {
                    state2.setImageResource(R.mipmap.img_zhong);
                    text_state2.setText("未处理");
                    text_state2.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
                } else if (event.getData().getList().get(1).getStatus().equals("1")) {
                    state2.setImageResource(R.mipmap.img_zhong);
                    text_state2.setText("发布任务中");
                    text_state2.setTextColor(getResources().getColor(R.color.label_yellow));
                } else if (event.getData().getList().get(1).getStatus().equals("2")) {
                    state2.setImageResource(R.mipmap.img_yes);
                    text_state2.setText("归档");
                    text_state2.setTextColor(getResources().getColor(R.color.label_green));
                }
                img_head2.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getList().get(1).getImg()));
                text_name2.setText(event.getData().getList().get(1).getName());
                text_time2.setText(event.getData().getList().get(1).getTimes());

//                //0未处理1同意2拒绝
//                if (event.getData().getList().get(2).getStatus().equals("0")) {
//                    state3.setImageResource(R.mipmap.img_zhong);
//                    text_state3.setText("未处理");
//                    text_state3.setTextColor(getResources().getColor(R.color.bottom_unchecked_color));
//                } else if (event.getData().getList().get(2).getStatus().equals("1")) {
//                    state3.setImageResource(R.mipmap.img_yes);
//                    text_state3.setText("发布任务中");
//                    text_state3.setTextColor(getResources().getColor(R.color.label_green));
//                } else if (event.getData().getList().get(2).getStatus().equals("2")) {
//                    state3.setImageResource(R.mipmap.img_yes);
//                    text_state3.setText("归档");
//                    text_state3.setTextColor(getResources().getColor(R.color.label_green));
//                }
//                img_head3.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getList().get(2).getImg()));
//                text_name3.setText(event.getData().getList().get(2).getName());
//                text_time3.setText(event.getData().getList().get(2).getTimes());

                if (event.getData().getImgs().isEmpty()) {
                    atask_list.setVisibility(View.GONE);
                } else {
                    atask_list.setVisibility(View.VISIBLE);
                    String[] img = event.getData().getImgs().split(",");
                    if (img.length == 1) {
                        img_photo1.setVisibility(View.VISIBLE);
                        img_photo2.setVisibility(View.INVISIBLE);
                        img_photo3.setVisibility(View.INVISIBLE);
                        img_photo1.setImageURI(Uri.parse(URLString.IMG_URL + img[0]));
                        img1=img[0];
                    } else if (img.length == 2) {
                        img_photo1.setVisibility(View.VISIBLE);
                        img_photo2.setVisibility(View.VISIBLE);
                        img_photo3.setVisibility(View.INVISIBLE);
                        img_photo1.setImageURI(Uri.parse(URLString.IMG_URL + img[0]));
                        img_photo2.setImageURI(Uri.parse(URLString.IMG_URL + img[1]));
                        img1=img[0];
                        img2=img[1];
                    } else if (img.length == 3) {
                        img_photo1.setVisibility(View.VISIBLE);
                        img_photo2.setVisibility(View.VISIBLE);
                        img_photo3.setVisibility(View.VISIBLE);
                        img_photo1.setImageURI(Uri.parse(URLString.IMG_URL + img[0]));
                        img_photo2.setImageURI(Uri.parse(URLString.IMG_URL + img[1]));
                        img_photo3.setImageURI(Uri.parse(URLString.IMG_URL + img[2]));
                        img1=img[0];
                        img2=img[1];
                        img3=img[2];
                    }
                }

                break;
            default:
                SN.SnackShow(text_state3, event.getMsg());
                break;
        }
    }

    public void podt(int status) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);
        hashMap.put("status", String.valueOf(status));//1通过，2拒绝
        new BaseInternet().getData(URLString.MANAGE_EAAPASSMD, hashMap, ForgetBean.class, new TaskApproveDetailsPostEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void TaskApproveDetailsPostEvent(TaskApproveDetailsPostEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, event.getMsg());
                finish();
                break;
            default:
                SN.SnackShow(text_state3, event.getMsg());
                break;
        }
    }

    public void podt2(int status) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        hashMap.put("id", id);
        hashMap.put("status", String.valueOf(status));//1通过，2拒绝
        new BaseInternet().getData(URLString.EAAPASSMA, hashMap, ForgetBean.class, new TaskApproveDetailsPostEvent<ForgetBean>(), false);
    }

    @EventAnnotation
    public void TaskApproveDetailsPostEvent2(TaskApproveDetailsPostEvent<ForgetBean> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, event.getMsg());
                finish();
                break;
            default:
                SN.SnackShow(text_state3, event.getMsg());
                break;
        }
    }

    @EventAnnotation
    public void ManageLeaveClickEvent2(ManageLeaveClickEvent2 event) {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }


}
